socket = require("socket")
json = require("json")

local t = {}
t.loop = -1
--if __FORM == nil then __FORM = arg end
if __FORM then
	t.time = __FORM.time
	t.uptime = __FORM.uptime
	t.loadavg1 = tonumber(__FORM.loadavg1)
	t.loadavg2 = tonumber(__FORM.loadavg2)
	t.loadavg3 = tonumber(__FORM.loadavg3)
	t.ftime = __FORM.time
	t.fuptime = __FORM.uptime
	t.floadavg1 = tonumber(__FORM.loadavg1)
	t.floadavg2 = tonumber(__FORM.loadavg2)
	t.floadavg3 = tonumber(__FORM.loadavg3)
	t.loop = 0
end	
--print(json.encode({previous=t}))
--while true do
	while true do
		local info = io.popen("uptime")
		local txt = info:read("*a")
		info:close()
		local _, _, hora, uptime, l1, l2, l3 = string.find(txt,"(%d+:%d+):%d+ up (.+), load average: ([0-9%.]+),%s([0-9%.]+),%s([0-9%.]*)")

		t.loop = t.loop + 1
		l1 = tonumber(l1)
		l2 = tonumber(l2)
		l3 = tonumber(l3)
		if t.time ~= hora
		or t.uptime ~= uptime
--		if t.uptime ~= uptime
		or t.loadavg1 ~= l1
		or t.loadavg2 ~= l2
		or t.loadavg3 ~= l3
		then
			t.time = hora
			t.uptime = uptime
			t.loadavg1 = l1
			t.loadavg2 = l2
			t.loadavg3 = l3
			break
		end
		socket.sleep(1)
	end
	print(json.encode({status=t}).."\r\n\r\n")
--end